<?php
class Dispatcher {
	private $requesturi;
	private $query;
	private $classname;
	private $functionname;
	private $functionparams;
	
	public function __construct() {
		$this->requesturi = $_SERVER['REQUEST_URI'];
		$this->query = $_SERVER['QUERY_STRING'];
		$this->dispatch();
	}
	
	/**
	 * Prepare the uri for dispatching
	 */
	private function prepare_dispatch_uri() {
		$base = $this->requesturi;														// Set base to the requesturi
		if(CONFIG_SERVER_URI) $base = str_replace(CONFIG_SERVER_URI, '', $base);		// If config_server_uri is set in the config, remove that from the base.
		if($this->query) return str_replace('?'.$this->query, '', $base);				// If there is a query set, remove that from the base to.
		return $base;
	}
	
	/**
	 * Dispatch the uri into the class, 
	 * function and/or functionparams
	 */
	private function dispatch() {
		$param_array = explode('/', $this->prepare_dispatch_uri());						// Split the URL on the forward slash.
		$this->classname = strtolower($param_array[0]);									// Extract the classname, save it and remove the value.
		unset($param_array[0]);															// Remove the classname key from the array, we don't need it.
		$this->functionname = $param_array[1];											// Extract the functionname, save it.
		unset($param_array[1]);															// Remove the functionname key from the array, we don't need it.
		$this->functionparams = array_filter($param_array);								// Extract the functionparams, filter and remove empty values.
	}
	
	public function __get($name)
	{
		if (in_array($name, array('classname','functionname','functionparams')))
		return $this->$name;
	}
	
	public function setFunctionname($value)	{ $this->functionname = $value; }
	public function setClassname($value)	{ $this->classname = $value; }
}

?>